<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-del="handleDel">
      <template #menu="scope">
        <ElButton type="text" size="small" icon="el-icon-view" @click="readMessageClick(scope.row, scope.index)">
          标记为已读
        </ElButton>
        <ElButton type="text" size="small" icon="el-icon-view" @click="openClick(scope.row, scope.index)">
          打开消息请求
        </ElButton>
      </template>
      <template #menuForm>
        <ElButton icon="el-icon-reading" type="primary" @click="flagMessageStatus()">标记为已读</ElButton>
      </template>
    </FyCurd>
  </BasicContainer>
</template>

<script>
  import { delObj, readMessage, systemMessagePage } from '@/api/message/systemmessagerecord'
  import { tableOption2 } from '@/const/crud/message/systemmessagerecord'
  import { mapActions, mapState } from 'vuex'
  import { services } from '@/config'

  export default {
    name: 'SystemMessage',
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          current: 1,
          size: 20,
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption2,
        form: {}
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      ...mapState('message', ['systemMessateList']),
      ...mapState('user', { userInfo: state => state.sysUser }),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.message_systemmessagerecord_add, false),
          delBtn: this.$isEmpty(this.permissions.message_systemmessagerecord_del, false),
          editBtn: this.$isEmpty(this.permissions.message_systemmessagerecord_edit, false)
        }
      }
    },
    mounted() {
      this.getList(this.page)
    },
    methods: {
      ...mapActions('message', ['fetchMessage']),
      // 列表查询
      getList(page) {
        systemMessagePage({
          current: page.currentPage,
          size: page.size,
          createdId: this.userInfo.userId,
          status: 0
        }).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
        })
      },
      // 删除
      handleDel: function (row, index) {
        this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.$message.success('删除成功')
            this.getList(this.page)
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      //  导出excel
      exportExcel() {
        this.$downBlobFile(
          `${services.adminService}systemmessagerecord/export`,
          this.searchForm,
          'systemmessagerecord.xlsx'
        )
      },
      async readMessageClick(row) {
        await readMessage(row.id).then(response => {
          if (response.data.data) {
            this.$message.success('标记成功')
          }
        })
        await this.fetchMessage()
        this.refreshChange()
      },
      openClick(row) {
        if (this.$isBlank(row.openUrl)) {
          this.$message.success('无请求连接！')
          return
        }
        if (row.typeCode === 'Y') {
          window.open(row.openUrl, '_blank')
        } else {
          // 路由地址
          this.$router.push({ path: row.openUrl })
        }
        this.$emit('handleClose')
      },
      async flagMessageStatus() {
        await readMessage(this.form.id).then(response => {
          if (response.data.data) {
            this.$message.success('标记成功')
            this.refreshChange()
            this.$refs.crud.closeDialog()
          }
        })
        await this.fetchMessage()
      }
    }
  }
</script>
